package com.lpfy.dtxc.utils

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.ConnectivityManager.NetworkCallback
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Build
import androidx.annotation.RequiresApi
import com.google.android.gms.common.util.CollectionUtils
import com.lpfy.dtxc.bean.NetworkStatus
import java.util.concurrent.CopyOnWriteArrayList

/**
 * @author: hanGuangHui
 * @data: 2024/3/18
 * @description:
 */
@SuppressLint("StaticFieldLeak")
object NetworkListenerHelper {
    private const val NET_TAG = "NetworkListenerHelper"
    private var mContext: Context? = null

    @SuppressLint("StaticFieldLeak")
    @Volatile
    private var mListenerList: CopyOnWriteArrayList<NetworkConnectedListener>? = null
    @RequiresApi(Build.VERSION_CODES.N)
    fun registerNetworkListener() {
        val connectivityManager =
            mContext!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        connectivityManager.registerDefaultNetworkCallback(MyNetworkCallback())
    }


    private fun notifyAllListeners(
        isConnected: Boolean,
        networkStatus: NetworkStatus
    ) {
        if (!CollectionUtils.isEmpty(mListenerList)) {
            for (listener in mListenerList!!) {
                listener?.onNetworkConnected(isConnected, networkStatus)
            }
        }
    }


    @Synchronized
    fun addListener(listener: NetworkConnectedListener?) {
        if (listener == null) {
            return
        }
        if (mListenerList == null) {
            mListenerList = CopyOnWriteArrayList()
        }
        // 防止重复添加
        if (!mListenerList!!.contains(listener)) {
            mListenerList!!.add(listener)
        }
    }


    @Synchronized
    fun removeListener(listener: NetworkConnectedListener?) {
        if (listener != null && !CollectionUtils.isEmpty(mListenerList)) {
            mListenerList!!.remove(listener)
        }
    }

    fun unregisterNetworkCallback() {
        if (mContext == null) {
            return
        }
        val connectivityManager = mContext!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        connectivityManager.unregisterNetworkCallback(NetworkCallback())
    }

    interface NetworkConnectedListener {
        fun onNetworkConnected(
            isConnected: Boolean,
            networkStatus: NetworkStatus?
        )
    }

    @SuppressLint("NewApi")
    private class MyNetworkCallback : NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            val netWorkState = getNetWorkState(mContext!!)
            notifyAllListeners(true, netWorkState)
        }

        override fun onLost(network: Network) {
            super.onLost(network)
            val netWorkState = getNetWorkState(mContext!!)
            notifyAllListeners(false, netWorkState)
        }

        override fun onCapabilitiesChanged(
            network: Network,
            networkCapabilities: NetworkCapabilities
        ) {
            super.onCapabilitiesChanged(network, networkCapabilities)
            if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                when {
                    networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
                    }
                    networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
                    }
                    else -> {
                    }
                }
            }
        }
    }

    fun init(context: Context): NetworkListenerHelper {
        mContext = context
        return this
    }
}